package UI;

import java.util.*;
import Controller.*;

public class LoanUI
{
    private LoanCtr loanCtr;
    private static LoanUI instance = null;
    private MainUI mainUI;
    
    private LoanUI()
    {
        loanCtr = loanCtr.getInstance();
        loanMenuStart();
    }
    
    public static LoanUI getInstance()
    {
        if(instance == null)
        {
            instance = new LoanUI();
        }
        return instance;
    }
    
    // starts loan menu
    public void loanMenuStart()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = printLoanMenu();
            switch (choice)
            {
                case 1: loanDvd();
                        break;
                case 2: findByPerson();
                        break;
                case 3: findByCopy();
                        break;
                case 4: updatePeriod();
                        break;
                case 5: returnCopy();
                        break;
                case 6: goBack();
                        break;
            }
        }
    }
    
    public int printLoanMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        //System.out.print('\f'); 
        System.out.println("*** Loan Menu ***");
        System.out.println("1. Loan DVD");
        System.out.println("2. Find Loan by Person");
        System.out.println("3. Find Loan by Copy");
        System.out.println("4. Update Period");
        System.out.println("5. Return Copy");
        System.out.println("6. Go Back");
        System.out.println("Your choice #: ");
        int choice = keyboard.nextInt();
        return choice;
    }
    
    public void loanDvd()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print('\f'); 
        System.out.println("Person ID:");
        int personId = Integer.parseInt(keyboard.nextLine());
        System.out.println("Copy Serial Number:");
        String serialNumber = keyboard.nextLine();
        System.out.println("Borrow Date:");
        String borrowDate = keyboard.nextLine();
        System.out.println("Period:");
        String period = keyboard.nextLine();
           
        loanCtr.createLoan(borrowDate, period, personId, serialNumber);
    }
    
    public void findByPerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print('\f'); 
        System.out.println("Person ID:");
        int personId = Integer.parseInt(keyboard.nextLine());
        
        System.out.println(loanCtr.findByPerson(personId));
    }
    
    public void findByCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print('\f');
        System.out.println("Copy Serial Number:");
        String serialNumber = keyboard.nextLine();
        
        System.out.println(loanCtr.findByCopy(serialNumber));
    }
    
    public void updatePeriod()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print('\f'); 
        System.out.println("Loan ID:");
        int loanId = Integer.parseInt(keyboard.nextLine());
        System.out.println("New Period:");
        String newPeriod = keyboard.nextLine();
        
        loanCtr.updateLoanPeriod(loanId,newPeriod);
    }
    
    public void returnCopy()
    {
       Scanner keyboard = new Scanner(System.in);
       System.out.print('\f'); 
       System.out.println("Loan ID:");
       int loanId = Integer.parseInt(keyboard.nextLine()); 
       
       loanCtr.endLoan(loanId);
    }
    
    // calls and returns to main menu
    public void goBack()
    {
        mainUI.getInstance().mainMenuStart();
    }
    
    
}
